using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.WII.TPL
{
    public partial class TPL : Form
    {
        string file;
        libWiiSharp.TPL b;
        public TPL(string file)
        {
            this.file = file;
            InitializeComponent();
        }

        private void BTI_Load(object sender, EventArgs e)
        {
            b = new libWiiSharp.TPL();
            b.LoadFile(file);
            for (int i = 0; i < b.NumOfTextures; i++)
            {
                toolStripComboBox1.Items.Add(i + 1);
            }
            toolStripComboBox1.SelectedIndex = 0;
            pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                if (b.GetTextureFormat(toolStripComboBox1.SelectedIndex) != libWiiSharp.TPL_TextureFormat.CMP)
                {
                    b.CreateFromImage(bi, b.GetTextureFormat(toolStripComboBox1.SelectedIndex));
                }
                else
                {
                    b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.RGBA8);
                }
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void i4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.I4);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void i8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.I8);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void iA4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.IA4);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void iA8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.IA8);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void rGB565ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.RGB565);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void rGB5A5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.RGB5A3);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void rGBA8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.RGBA8);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void cI4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.CI4);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void cI8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.CreateFromImage(bi, libWiiSharp.TPL_TextureFormat.CI8);
                pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            b.Save(file);
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = b.ExtractTexture(toolStripComboBox1.SelectedIndex);
        }
    }
}
